$(function() {
	$( "#global-search" ).autocomplete({
		source: function( request, response ) {
			jQuery.ajax({
				type: "POST",
				url: BASEURL + "ajax/global_search.php",
				data: ({maxrow:12,keyword:request.term}),
				dataType: "json",
				cache: false,
				success: function(data){
					response( $.map( data.hotels, function( item ) {
						return {
							label: item.name + (item.location ? ", " + item.location : ""),
							value: item.name,
							hotel_slug: item.hotel_slug,
							destination_slug: item.destination_slug
						}
					}));
				}
			});
		},
		minLength: 2,
		select: function( event, ui ) {
			$('#hotel-slug').val(ui.item.hotel_slug);
			$('#destination-slug').val(ui.item.destination_slug);
		},
		open: function() {
			$( this ).removeClass( "ui-corner-all" ).addClass( "ui-corner-top" );
		},
		close: function() {
			$( this ).removeClass( "ui-corner-top" ).addClass( "ui-corner-all" );
		}
	});
});